import os
from . import settings
from celery import Celery
from celery.app.control import Control, Inspect

# 加载配置
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'conf.settings')
# 实例化
app = Celery(settings.CELERY_APP_NAME)

# Using a string here means the worker doesn't have to serialize
# the configuration object to child processes.
# - namespace='CELERY' means all celery-related configuration keys
#   should have a `CELERY_` prefix.
# namespace='CELERY'得作用是允许你在Django配置文件中对CELERY前缀的配置
# 如果没有前缀则省略，一般情况下Celery配置项必须以CELERY开头，防止冲突
app.config_from_object('django.conf:settings', namespace='CELERY')

# 自动发现项目中的tasks
app.autodiscover_tasks()


# celery_control: Control = Control(app=app)
#
# celery_inspect: Inspect = celery_control.inspect()

# 一个测试任务
@app.task(bind=True)
def debug_task(self):
    print(f'Request: {self.request!r}')
